import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store/store";

Vue.use(VueRouter);
// wly
import Shop from "../components/wly/Shop";
import ShopTwo from "../components/wly/Shoptwo";
import ShopThree from "../components/wly/ShopThree";

// yjl
import Ypersonal from "../components/yjl/Ypersonal";
import codetest from "../components/yjl/codetest";
// czl
import Home from "../components/czl/Home";
import Business from "../components/czl/Business";
import Login from "../components/czl/Login";

let router = new VueRouter({
  routes: [
    // wly
    {
      name: "Shop",
      path: "/shop",
      component: Shop,
      meta: {
        memoryRouter: true, //是否记忆当前路由为常用工具
        requireAuth: false, // 是否需要登录访问 true是 false否
        keepAlive: true, // 是否使用 keep-alive 缓存页面
      },
    },
    {
      name: "ShopTwo",
      path: "/shopTwo",
      component: ShopTwo,
    },
    {
      name: "ShopThree",
      path: "/shopThree",
      component: ShopThree,
    },
    // yjl
    {
      name: "ypersonal",
      path: "/ypersonal",
      component: Ypersonal,
    },
    {
      name: "codetest",
      path: "/codetest",
      component: codetest,
    },

    // czl
    {
      name: "Home",
      path: "/",
      component: Home,
    },
    {
      name: "Login",
      path: "/login",
      component: Login,
      meta: {
        memoryRouter: true, //是否记忆当前路由为常用工具
        requireAuth: false, // 是否需要登录访问 true是 false否
        keepAlive: true, // 是否使用 keep-alive 缓存页面
      },
    },
    {
      name: "Business",
      path: "/business",
      component: Business,
    },
  ],
});

// 全局路由守卫

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth === true && !getToken()) {
    next({
      name: "Login",
    });
  } else {
    // 记忆最近访问过的常用功能====================
    const commonTools = localStorage.getItem("commonTools")
      ? JSON.parse(localStorage.getItem("commonTools"))
      : new Array();
    const routerData = new Object();
    routerData.router = new Object();
    routerData.router.name = to.name;
    routerData.router.params = to.params;
    routerData.router.query = to.query;
    routerData.title = to.meta.pageTitle;
    routerData.name = to.name;
    let flag = false;
    for (let key in commonTools) {
      if (commonTools[key].name === to.name && key < 3) {
        flag = true;
      }
    }
    if (!flag && routerData.title && to.meta.memoryRouter) {
      commonTools.splice(0, 0, routerData);
    }
    router.app.$options.store.commit("comm", commonTools);
    console.log(router.app.$options.store.state.city);
    //  store.commit('comm', commonTools);
    // 记忆最近访问过的常用功能====================end
    /*记忆当前路由信息，用于token失效后，重新登录时回到当前页面*/
    // 缓存当前路由名称，登录成功后自动跳转到该路由页面
    //   if(to.name !== "Login") {
    //     let memoryRouter = new Object();
    //     memoryRouter.name = to.name;
    //     memoryRouter.params = to.params;
    //     memoryRouter.query = to.query;
    //     SetAutoJumpRouteName(memoryRouter);
    // }else{
    //     sessionStorage.removeItem("autoJumpRouteName");
    // }
    //实时更新用户信息
    // sendGetUserInfo(()=>{})
    next();
  }
});
export default router;
